package com.dam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dam.entity.AlgType;
import com.dam.entity.Algorithm;
import com.dam.entity.User;
import com.dam.entity.vo.AlgorithmDisplay;
import com.dam.mapper.AlgorithmMapper;
import com.dam.service.AlgTypeService;
import com.dam.service.AlgorithmService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dam.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author dam
 * @since 2021-11-03
 */
@Service
public class AlgorithmServiceImpl extends ServiceImpl<AlgorithmMapper, Algorithm> implements AlgorithmService {

    @Autowired
    private AlgorithmService algorithmService;

    @Autowired
    private AlgTypeService algTypeService;

    @Autowired
    private UserService userService;

    @Override
    public List<AlgorithmDisplay> queryAlgorithmDisplayList() {
        List<Algorithm> algorithmDisplayList = algorithmService.list(null);
        List<AlgorithmDisplay> algorithmDisplays = new ArrayList<>();

        for (int i = 0; i < algorithmDisplayList.size(); i++) {
            AlgorithmDisplay algorithmDisplay = new AlgorithmDisplay();
            BeanUtils.copyProperties(algorithmDisplayList.get(i), algorithmDisplay);

            AlgType algType = algTypeService.getById(algorithmDisplayList.get(i).getTypeId());
            algorithmDisplay.setAlgorithmType(algType.getTypeName());

            User user = userService.getById(algorithmDisplayList.get(i).getUserId());
            algorithmDisplay.setAlgorithmAuthorName(user.getName());

            algorithmDisplays.add(algorithmDisplay);
        }

        return algorithmDisplays;
    }

    @Override
    public void queryAlgorithmDisplayListWithWrapper(Page<AlgorithmDisplay> pageAlgorithm, QueryWrapper<Algorithm> wrapper) {
        List<Algorithm> algorithmDisplayList = algorithmService.list(wrapper);
        List<AlgorithmDisplay> algorithmDisplays = new ArrayList<>();

        for (int i = 0; i < algorithmDisplayList.size(); i++) {
            AlgorithmDisplay algorithmDisplay = new AlgorithmDisplay();
            BeanUtils.copyProperties(algorithmDisplayList.get(i), algorithmDisplay);

            AlgType algType = algTypeService.getById(algorithmDisplayList.get(i).getTypeId());
            algorithmDisplay.setAlgorithmType(algType.getTypeName());

            User user = userService.getById(algorithmDisplayList.get(i).getUserId());
            algorithmDisplay.setAlgorithmAuthorName(user.getName());

            algorithmDisplays.add(algorithmDisplay);
        }

        pageAlgorithm.setRecords(algorithmDisplays);
        pageAlgorithm.setTotal(algorithmDisplays.size());
    }
}
